#!/usr/bin/env python
# coding: utf-8

# In[21]:


def division():
    apple = int(input('please enter your number of apples: '))
    children = int(input('please enter your number of children '))
    result = apple // children 
    remain = apple % children #取模，就是求余数
    if remain == 0:
        print(apple,'个苹果，分给了',children,'个小朋友，每人分',result,'个')
    else:
        print(apple,'个苹果，分给了',children,'个小朋友，每人分',result,'个，剩了',remain,'个')

try:
    division()
except ZeroDivisionError:
    print('除零错误。')
except ValueError:
    print('\n数据输入错误。')
print('congratulations!')                                                                                                                                                                                                                                                                                                                         


# In[23]:


class fewAppleError(Exception):
    errorhint = 'too few apples...'



def division():
    apple = int(input('please enter your number of apples: '))
    children = int(input('please enter your number of children '))
    assert apple >= children
    # if apple < children:
     #   raise fewAppleError
    result = apple // children 
    remain = apple % children #取模，就是求余数
    if remain > 0:
        print(apple,'个苹果，分给了',children,'个小朋友，每人分',result,'个')
    else:
        print(apple,'个苹果，分给了',children,'个小朋友，每人分',result,'个，剩了',remain,'个')

try:
    division()
except (ZeroDivisionError,ValueError) as e:
    print('输入错误。', e)
except fewAppleError as tfa:
    print(tfa.errorhint)


# In[24]:


def division():
    apple = int(input('please enter your number of apples: '))
    children = int(input('please enter your number of children '))
    result = apple // children 
    remain = apple % children #取模，就是求余数
    if remain == 0:
        print(apple,'个苹果，分给了',children,'个小朋友，每人分',result,'个')
    else:
        print(apple,'个苹果，分给了',children,'个小朋友，每人分',result,'个，剩了',remain,'个')

try:
    division()
except (ZeroDivisionError,ValueError) as e:
    print('输入错误。')
else:
    print('congratulations!')   
finally:
    print('无论如何，我尽力了')


# In[25]:


#全局变量，局部变量
def t():
    a = 7
    print(a)
t()


# In[27]:


def t():
    global a
    a = 7
    print(a)
a = 6 #q全局变量  
t()
print(a)


# In[35]:


L = []
L = list(range(1,100,2))
print(L)


# In[8]:


L = list(range(100))
L = L[1::2]
print(L)
L1 = L[-2:]


L1 = L[:10]
L1 = L[-10:]
L1 = L[10:20]
L1 = L[:10:2]
L1 = L[::5]
L1 = L[1::2]
L1 = L[::2]

print(L1)


# In[9]:


(0,1,2,3,4,5)[:3]


# In[10]:


#开发一个工具，可去掉首尾空格
def trim(s):
    #如果s为空，s == ''则无意义去空格
    if s == '': 
        return s
    #如果s首尾都无空格，则返回s
    if s[0] != ' ' and s[-1] != ' ':
        return s
    #如果首部有空格
    if s[0] == ' ':
        return trim(s[1:])
    #如果尾部有空格
    if s[-1] == ' ':
        return trim(s[:-1])
print(trim('   abc   edf  g  '))


# In[26]:


d = {'a':1, 'b':2, 'c':3}
for key in d:
    print(key)

for value in d.values():
    print(value)
for item in d.items():
    print(item)

for k,v in d.items():
    print(k,v)


# In[24]:


def f():
    a = 1
    b = 2
    return a,b

result = ()
result = f()
print(result)


# In[39]:


from collections.abc import Iterable
print(isinstance('abc',Iterable))
print(isinstance([1,2,3],Iterable))
print(isinstance(123,Iterable))
print(isinstance(range(1,100),Iterable))


# In[41]:


for index, value in enumerate(['1','2','3']):
    print(index, value)


# In[44]:


def findMinAndMax(L):
    #先将第一个数作为最大值最小值初始值，依次遍历list，发现更大更小的则更新
    if len(L) > 0:  #有实际的值才可、
        maxv = L[0]
        minv = L[0]
        for x in L[1:]:
            if x > maxv:
                maxv = x
            if x < minv:
                minv = x
        return maxv, minv
    else:
        return None, None
print(findMinAndMax([4,3,5,7]))   
print(findMinAndMax([1]))


# In[45]:


L = []
for x in range(1,11):
    L.append(x * x)
print(L)


# In[46]:


#列表生成式：x * x 表示列表中的数字形式，
L1 = [x * x for x in range(1,11)]
L2 = [x * x for x in range(2,11,2)]
print(L2)


# In[48]:


L2 = [x * x for x in range(1,11) if x % 2 == 0]
print(L2)


# In[52]:


#输出ABC和XYZ任意组合的两个字符串：AX,AY,AZ,BX,BY,BZ
#列表生成式，前一部分是元素形态，后一部分是基数的来源

L3 = []
for sub1 in 'ABC':
    for sub2 in 'XYZ':
        L3.append(sub1 + sub2)        
print(L3)

L3 = [sub1 + sub2 for sub1 in 'ABC' for sub2 in 'ABC']
print(L3)


# In[58]:


L1 = ['AAA','BBB',18,'ccc',None]
L2 = [s.lower() for s in L1 if isinstance(s,str)] #S是字符串的才输出小写，是字符串可迭代
print(L2)

L2 = []
for s in L1:
    if isinstance(s, str):
        L2.append(s.lower())
    else:
        L2.append(s)
print(L2)


# In[59]:


L1 = (x * x for x in range(1,11))
print(type(L1))


# In[63]:


#生成器也可以迭代
G = (x * x for x in range(1,11))
#print(type(L1))
num = next(G)
print(num)
num = next(G)
print(num)
num = next(G)
print(num)


# In[65]:


#生成器也可以迭代
G = (x * x for x in range(1,11))
for n in G:
    print(n)
print(isinstance(G, Iterable))


# In[70]:


#返回所有自然数的集合,有yield,就是生成器，可迭代
def num():
    i = 0
    while 1:
        yield i 
        i = i + 1
x = num()
#print(type(x))
print(next(x))
print(next(x))
print(next(x))


for n in x:
    print(n)
    if n > 9:
        break


# In[71]:


#法1
def Ruigenum():
    for i in range(1000,10000):
        a = i % 10
        b = i // 10 % 10
        c = i // 100 % 10
        d = i // 1000 % 10
        if i == a ** 4 + b ** 4 + c ** 4 + d ** 4:
            yield i
x = Ruigenum()
for num in x:
    print(num)
        
        


# In[72]:


#法2
def Ruigenum():
    for i in range(1000,10000):
        a = i % 10 #个位
        b = i // 10 % 10 # 十位
        c = i // 100 % 10
        d = i // 1000 % 10
        if i == a ** 4 + b ** 4 + c ** 4 + d ** 4:
            yield i
x = Ruigenum()
while 1:
    try:
        num = next(x)
        print(num)
    except StopIteration:
        break


# In[74]:


#法3,
def Ruigenum():
    for i in range(1000,10000):
        sum = 0
        for x in str(i):
            sum += int(x) ** 4
        if i == sum:
            yield i 
x = Ruigenum()
while 1:
    try:
        num = next(x)
        print(num)
    except StopIteration:
        break
        


# In[77]:


#求两个数的level 次方和，对几次方和的改进
def powersum(a,b,level):
    return a ** level + b ** level
print(powersum(1,4,3))


# In[79]:


def power(a):
    return a ** 2

L = [1,2,3,4,5,6,7,8,9]
r = map(power, L) 
print(list(r))
    


# In[80]:


def power(a):
    return a ** 2
L = [1,2,3,4,5,6,7,8,9]
L1 = []
for x in L:
    L1.append(power(x))
print(L1)


# In[81]:


def power3(a):
    return a ** 3

L = [1,2,3,4,5,6,7,8,9]
r = map(power3, L)  
print(list(r))


# In[85]:


def power(a):
    return a ** 2
def power3(a):
    return a ** 3

def powercon(a):
    if a % 2 == 0:
        result = power3(a)
    else:
        result = power(a)
    return result

L = [1,2,3,4,5,6,7,8,9]
r = map(powercon, L) 
print(list(r))


# In[86]:


#把[1,3,5,7,9]变成整数13579
from functools import reduce
def fun(x, y):
    return x * 10 + y
reduce(fun, [1,3,5,7,9]) #将操作依次作用在前两个元素上，得到的结果为新的元素，再和下一个元素作用，类似于滚雪球。


# In[87]:


def is_odd(n):
    return n % 2 == 1
x = filter(is_odd,[1,2,4,5,6,9,10,15])
print(list(x))
print(next(x))
print(next(x))
for num in x:
    print(num)


# In[ ]:




